package Document;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class Win extends JFrame{
    JTextArea input,show;
    JMenuBar bar;
    JMenu menu;
    JMenuItem itemcopy,itemcut,itemPaste;
    TextMonitor textListener;
    HandleMonitor handListener;
    Win(){
        init();
        setVisible(true);
        setLayout(new FlowLayout());
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    private void init() {
        input=new JTextArea(15,20);
        show=new JTextArea(15,20);
        show.setLineWrap(true);//自动换行
        bar=new JMenuBar();
        menu=new JMenu("编辑");
        itemcopy=new JMenuItem("复制");
        itemcut=new JMenuItem("剪切");
        itemPaste=new JMenuItem("粘贴");
        itemcopy.setAccelerator(KeyStroke.getKeyStroke('c'));//设置快捷方式
        itemcut.setAccelerator(KeyStroke.getKeyStroke('z'));//设置快捷方式
        itemPaste.setAccelerator(KeyStroke.getKeyStroke('p'));//设置快捷方式
        itemcopy.setActionCommand("copy");
        itemcut.setActionCommand("cut");
        itemPaste.setActionCommand("paste");
        menu.add(itemcopy);
        menu.add(itemcut);
        menu.add(itemPaste);
        bar.add(menu);
        setJMenuBar(bar);
        add(new JScrollPane(input));
        add(new JScrollPane(show));
        textListener=new TextMonitor();
        handListener=new HandleMonitor();
        textListener.setText(input);
        textListener.setOutput(show);
        handListener.setText(input);
        handListener.setOutput(show);
        (input.getDocument()).addDocumentListener(textListener);//向文档设置监视器
        itemcopy.addActionListener(handListener);
        itemcut.addActionListener(handListener);
        itemPaste.addActionListener(handListener);
    }
}
class TextMonitor implements DocumentListener {
    JTextArea text,output;
    public void setText(JTextArea text) {
        this.text = text;
    }
    public void setOutput(JTextArea output) {
        this.output = output;
    }
    @Override
    public void insertUpdate(DocumentEvent e) {
        changedUpdate(e);
    }
    @Override
    public void removeUpdate(DocumentEvent e) {
        changedUpdate(e);
    }
    @Override
    public void changedUpdate(DocumentEvent e) {
        double num;
        double sum=0;
        double average = 0;
        int count=0;
        String str=text.getText();
        String regex="[\\s\\p{Punct}]+";
        String [] stack=str.split(regex);
        output.setText(null);
        for (int i = 0; i < stack.length; i++) {
            num=Double.parseDouble(stack[i]);
            sum+=num;
            count++;
            average = sum /count;
            if (count==1)
                output.append("\n");
            else
                output.append(" 和为:"+sum+" 平均值为："+average+"\n");
        }
    }
}
class HandleMonitor implements ActionListener {
    JTextArea text,output;
    public void setText(JTextArea text) {
        this.text = text;
    }
    public void setOutput(JTextArea output) {
        this.output = output;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        String  str=e.getActionCommand();
        if (str.equals("copy"))
            output.copy();
        else if (str.equals("cut"))
            output.cut();
        else if (str.equals("paste"))
            output.paste();
    }
}
public class DocumentCompute {
    public static void main(String[] args) {
        Win win=new Win();
        win.setBounds(200,200,500,400);
        win.setTitle("计算");
    }
}

